package com.ives.launcher.module;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;

import com.ives.launcher.R;
import com.ives.launcher.model.ItemInfo;
import com.ives.launcher.model.ItemInfo_App;
import com.ives.launcher.model.ItemInfo_Floder;
import com.ives.launcher.util.ImageTool;
import com.ives.launcher.view.View_Apps_Page_Root;
import com.ives.launcher.view.View_Apps_Root;
import com.ives.launcher.view.View_Cell;
import com.ives.launcher.view.View_Cell.MOnClickLinstener;
import com.ives.launcher.view.View_Menu_Bottom;
import com.ives.launcher.view.View_PageIndexBar;
import com.ives.launcher.view.View_Page_Item;
import com.ives.launcher.view.View_Page_Root;
import com.ives.launcher.view.View_Page_Root.OnScrollListener;
import com.ives.launcher.view.View_Root;

/**
 * 数据装载器 功能：负责加载各种数据，包括应用程序数据，用户数据等等
 * 
 * @author ives
 * 
 */
public class DataLoader extends Thread {
	private Context context;
	
	private View_Root view_root;
	private View_Page_Root page_root;
	private View_Apps_Root apps_root;
	private View_Apps_Page_Root apps_page_root;
	private int pagewidth = 4;
	private int pageheight = 4;
	
	private View_PageIndexBar pageIndexBar_desktop;
	private View_PageIndexBar pageIndexBar_app;
	private View_Menu_Bottom menu_bottom;
	
	
	public DataLoader(Context context,View_Root root, View_Page_Root page_root,
			View_Apps_Page_Root apps_page_root) {
		this.context = context;
		this.view_root = root;
		this.page_root = page_root;
		this.apps_page_root = apps_page_root;
	}

	public void startLoad() {
		loadAppsDatas();
		loadUserDatas();
		addlisntener();
		
		addTestFloder();
	}
	
	public void loadAppsDatas() {
		ArrayList<View_Cell> apps = getSystemApps();
		int count = apps.size();
		int index = 0;
		point:
		for (int i = 0; i < count/(pagewidth*pageheight)+2; i++) {
			View_Page_Item page = new View_Page_Item(context);
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
			page.setIndex(apps_page_root.getChildCount());
			page.setView_manager(view_root);
			apps_page_root.addView(page,params);
			for (int j = 0; j < pagewidth; j++) {// 列（x）
				for (int k = 0; k < pageheight; k++) {// 行（y）
					if(count==index)break point;
					View_Cell cell = apps.get(index);
					View_Page_Item.LayoutParams lp = new View_Page_Item.LayoutParams(k, j, 1, 1, 15, 15);
					cell.setLayoutParams(lp);
					page.addView(cell,lp);
					index++;
				}
			}
		}
	}
	// 读取所有应用
	ArrayList<View_Cell> apps = new ArrayList<View_Cell>();
	/**
	 * 获取系统所有已经安装的app
	 * @return
	 */
	private ArrayList<View_Cell> getSystemApps(){
		
		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		final PackageManager manager = context.getPackageManager();
		final List<ResolveInfo> allapps = manager.queryIntentActivities(mainIntent, 0);
		for (int i = 0; i < allapps.size(); i++) {
			ResolveInfo info = allapps.get(i);
			final ItemInfo_App minfo = new ItemInfo_App();
			minfo.icon = ImageTool.drawableToBitmap(info.activityInfo.loadIcon(manager));
			minfo.title = info.loadLabel(manager);
			minfo.intent = new Intent();
			minfo.intent.setClassName(info.activityInfo.packageName,info.activityInfo.name);
			minfo.itemType = ItemInfo.Type_App;
			final View_Cell cell = new View_Cell(context,minfo.title.toString(),ImageTool.getShowBitmap(minfo,context), 10f, 2f);
			cell.setItemInfo(minfo);
			cell.setmOnClickLinstener(new MOnClickLinstener() {
				@Override
				public void onClick() {
					
					context.startActivity(minfo.intent);
				}
			});
			apps.add(cell);
		}
		return apps;
	}
	private void addTestFloder(){
		//添加一个文件夹
		ItemInfo_Floder floder = new ItemInfo_Floder();
		for (int i = 0; i < apps.size(); i++) {
			if(floder.items.size()<3){
				floder.items.add(apps.get(i).getItemInfo());
			}
		}
		floder.title = "this is a floder";
		floder.itemType = ItemInfo.Type_Floder;
		//floder.customIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.allapps_mainbtn);
		View_Cell cell = new View_Cell(context,floder.title.toString(),ImageTool.getShowBitmap(floder,context), 10f, 2f);
		cell.setItemInfo(floder);
		View_Page_Item.LayoutParams lp = new View_Page_Item.LayoutParams(3, 3, 1, 1, 15, 15);
		cell.setLayoutParams(lp);
		View_Page_Item page = (View_Page_Item) page_root.getChildAt(0);
		page.addView(cell,lp);
		//给floder添加点击事件
		cell.setmOnClickLinstener(floder.makeOnClickLinstener(context,cell));
		
	}
	
	public void loadUserDatas() {
		/** 添加桌面上的数据 **/
		for (int k = 0; k < 5; k++) {
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
			View_Page_Item screen3 = new View_Page_Item(context);
			screen3.setLayoutParams(params);
			
			for (int i = 0; i < 4; i++) {// 列（x）
				for (int j = 0; j < 2; j++) {// 行（y）
					View_Cell v1 = new View_Cell(context, "this is a test",BitmapFactory.decodeResource(context.getResources(),R.drawable.test), 10f, 2f);
					View_Page_Item.LayoutParams lp1 = new View_Page_Item.LayoutParams(i, j, 1, 1, 15, 15);
					screen3.addView(v1, lp1);
				}
			}
			screen3.setIndex(page_root.getChildCount()+1);
			
			page_root.addView(screen3);
			screen3.setView_manager(view_root);
		}
		/********/
		pageIndexBar_desktop.setNumPages(page_root.getChildCount());
		pageIndexBar_app.setNumPages(apps_page_root.getChildCount());
		/** 给底部菜单添加数据 **/
		for (int i = 0; i < 4; i++) {// 列（x）
			for (int j = 0; j < 1; j++) {// 行（y）
				final View_Cell v1 = new View_Cell(context, "this is a test",BitmapFactory.decodeResource(context.getResources(),R.drawable.test), 10f, 2f);
				View_Page_Item.LayoutParams lp1 = new View_Page_Item.LayoutParams(i, j, 1, 1, 5, 5);
				menu_bottom.addView(v1, lp1);
				if(i==2){
					v1.setIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.allapps_mainbtn));
					v1.setmOnClickLinstener(new MOnClickLinstener() {
						@Override
						public void onClick() {
							apps_root.setVisibility(View.VISIBLE);
						}
					});
			}
		}
		menu_bottom.setIconXGap(5);
		menu_bottom.setIconYGap(5);
		menu_bottom.setIndex(10);
		menu_bottom.setView_manager(view_root);
		}
	}

	private void addlisntener(){
		apps_page_root.addOnScrollListener(new OnScrollListener() {
			@Override
			public void onScroll(int scrollX) {
				float scale = (float) (apps_page_root.getWidth() * apps_page_root.getChildCount())/ (float) pageIndexBar_app.getWidth();
				
				pageIndexBar_app.setPosition((int) (scrollX / scale));
			}

			@Override
			public void onViewScrollFinished(int currentPage) {
				pageIndexBar_app.setCurrentPage(currentPage);
			}
		});
		page_root.addOnScrollListener(new View_Page_Root.OnScrollListener() {
			@Override
			public void onScroll(int scrollX) {
				float scale = (float) (page_root.getWidth() * page_root.getChildCount())*page_root.getScaleRatioX()/ ((float) pageIndexBar_desktop.getWidth()*10);
				pageIndexBar_desktop.setPosition((int) (scrollX / scale));
			}

			@Override
			public void onViewScrollFinished(int currentPage) {
				pageIndexBar_desktop.setCurrentPage(currentPage);
			}
		});
	}
	@Override
	public void run() {

	}
	/**getter and setter**/
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public View_Page_Root getPage_root() {
		return page_root;
	}

	public void setPage_root(View_Page_Root page_root) {
		this.page_root = page_root;
	}


	public int getPagewidth() {
		return pagewidth;
	}

	public void setPagewidth(int pagewidth) {
		this.pagewidth = pagewidth;
	}

	public int getPageheight() {
		return pageheight;
	}

	public void setPageheight(int pageheight) {
		this.pageheight = pageheight;
	}

	public View_PageIndexBar getPageIndexBar_desktop() {
		return pageIndexBar_desktop;
	}

	public void setPageIndexBar_desktop(View_PageIndexBar pageIndexBar_desktop) {
		this.pageIndexBar_desktop = pageIndexBar_desktop;
	}

	public View_PageIndexBar getPageIndexBar_app() {
		return pageIndexBar_app;
	}

	public void setPageIndexBar_app(View_PageIndexBar pageIndexBar_app) {
		this.pageIndexBar_app = pageIndexBar_app;
	}

	public View_Menu_Bottom getMenu_bottom() {
		return menu_bottom;
	}

	public void setMenu_bottom(View_Menu_Bottom menu_bottom) {
		this.menu_bottom = menu_bottom;
	}

	public View_Root getView_root() {
		return view_root;
	}

	public void setView_root(View_Root view_root) {
		this.view_root = view_root;
	}

	public View_Apps_Root getApps_root() {
		return apps_root;
	}

	public void setApps_root(View_Apps_Root apps_root) {
		this.apps_root = apps_root;
	}

	public View_Apps_Page_Root getApps_page_root() {
		return apps_page_root;
	}

	public void setApps_page_root(View_Apps_Page_Root apps_page_root) {
		this.apps_page_root = apps_page_root;
	}
	

	
}
